package com.hp.btc.util.ftp;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SendFile {
	
	private static final Logger logger = LoggerFactory.getLogger(SendFile.class);
		
	private String ipDestinity;
	private String pathDestinationFTP;
	private String usuFTP;
	private String passFTP;
	private String portFTP;
	
	public SendFile(){}
	
	public SendFile(String ipDestinity, String pathDestinationFTP,
			String usuFTP, String passFTP, String portFTP) {
		this.ipDestinity = ipDestinity;
		this.pathDestinationFTP = pathDestinationFTP;
		this.usuFTP = usuFTP;
		this.passFTP = passFTP;
		this.portFTP = portFTP;
	}
	
	public void sendFileFTPServers(String baseFolder,String nameFile, String ipFilter) throws Exception{
		logger.info("sendFileFTPServers  baseFolder="+baseFolder+" nameFile="+nameFile);
		File file = new File(baseFolder, nameFile);		
				
		List<String> destinations = Arrays.asList(ipDestinity.split(","));
		SendFileByFTP sendFileByFTP = null;
		
		for (String destination : destinations) {
			String ipDestination = destination;
			if(ipDestination.trim().equalsIgnoreCase(ipFilter.trim())){
				sendFileByFTP = null;
				continue;
			}
			
			logger.info(" Servidor " + destination);
			
        	try {
        		sendFileByFTP = new SendFileByFTP(usuFTP, passFTP, ipDestination, portFTP);
        	    sendFileByFTP.sendFileData(pathDestinationFTP, file);	
        	    logger.info("file tranfered... ok");
        	    
	        } catch (IOException exception) {
	        	logger.error(" metodo sendFileFTPServers: " +exception);
	        }finally{
	        	if(sendFileByFTP!=null)
	        		sendFileByFTP.close();
	        }
        }
	}
	
	public void sendFileSFTPServers(String baseFolder,String nameFile, String ipFilter)  throws Exception{
		logger.info("sendFileSFTPServers  baseFolder="+baseFolder+" nameFile="+nameFile+" exclude="+ipFilter);
		File file = new File(baseFolder, nameFile);		
				
		List<String> destinations = Arrays.asList(ipDestinity.split(","));
		
		SendFileBySFTP sendSftp = null;
		for (String destination : destinations) {
			String ipDestination = destination;
			if(ipDestination.trim().equalsIgnoreCase(ipFilter.trim())){
				sendSftp = null;
				continue;
			}
			logger.info(" Servidor: " + ipDestination);
			
			try{        		
        		sendSftp = new SendFileBySFTP(usuFTP, passFTP, ipDestination.trim(),portFTP);        			      	        
        		sendSftp.sendSftpFile(file, pathDestinationFTP);
        		logger.info("file tranfered... ok");
			}catch (IOException exception) {
	        	logger.error(" metodo sendFileSFTPServers: " ,exception);
	        }finally{
	        	if(sendSftp!=null)
	        		sendSftp.close();
	        }
		}
		
	}

	public String getIpDestinity() {
		return ipDestinity;
	}

	public void setIpDestinity(String ipDestinity) {
		this.ipDestinity = ipDestinity;
	}

	public String getPathDestinationFTP() {
		return pathDestinationFTP;
	}

	public void setPathDestinationFTP(String pathDestinationFTP) {
		this.pathDestinationFTP = pathDestinationFTP;
	}

	public String getUsuFTP() {
		return usuFTP;
	}

	public void setUsuFTP(String usuFTP) {
		this.usuFTP = usuFTP;
	}

	public String getPassFTP() {
		return passFTP;
	}

	public void setPassFTP(String passFTP) {
		this.passFTP = passFTP;
	}

	public String getPortFTP() {
		return portFTP;
	}

	public void setPortFTP(String portFTP) {
		this.portFTP = portFTP;
	}

}
